using UnityEngine;
using System.Collections;

public class AnimationSpriteSript : MonoBehaviour {

	
	public int uvAnimationTileX; 

	public int uvAnimationTileY; 
	public float framesPerSecond;

	private bool activate = false;
	private float fSaveTime;
    private bool stop;


	void Start()
	{
		reset();
        stop = false;
	}

	void LateUpdate () {

		if (!activate)
			reset();


        if (!stop)
        {
            int index = (int)((Time.time - fSaveTime) * framesPerSecond);
            // repeat when exhausting all frames
            index = index % (uvAnimationTileX * uvAnimationTileY);

            // Size of every tile
            Vector2 size = new Vector2((float)(1.0 / uvAnimationTileX), (float)(1.0 / uvAnimationTileY));

            int uIndex = index % uvAnimationTileX;
            int vIndex = index / uvAnimationTileX;

            Vector2 offset = new Vector2((float)(uIndex * size.x), (float)(1.0 - size.y - vIndex * size.y));

            renderer.material.SetTextureOffset("_MainTex", offset);
            renderer.material.SetTextureScale("_MainTex", size);
        }
	}


	public void reset()
	{
		fSaveTime = Time.time;
		activate = true;
	}

	public void changeActivate()
	{
		activate = false;
	}

    public void Stop()
    {
        stop = true;
    }

	public void relaunch()
	{
		stop = false;
	}

}

